package com.sntk.organauthomanager.infrastructure.dao;

import com.sntk.organauthomanager.domain.model.AffiliateInfo;
import com.sntk.organauthomanager.domain.model.UserAffiliate;
import com.sntk.organauthomanager.domain.repository.UserAffiliateRepository;
import com.sntk.organauthomanager.infrastructure.convert.OrganizationConvert;
import com.sntk.organauthomanager.infrastructure.db.entity.OrganizationEntity;
import com.sntk.organauthomanager.infrastructure.db.entity.UserAffiliateEntity;
import com.sntk.organauthomanager.infrastructure.db.repository.OrganizationDBRepository;
import com.sntk.organauthomanager.infrastructure.db.repository.UserAffiliateDBRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @ClassName: UserAffiliateRepositoryImpl
 * @author: wgchao
 * @createTime: 2020/9/9 9:55 AM
 * @Description: TODO
 */
@Component
public class UserAffiliateRepositoryImpl implements UserAffiliateRepository {
    @Autowired
    private UserAffiliateDBRepository userAffiliateDBRepository;
    @Autowired
    private OrganizationDBRepository organizationDBRepository;
    @Autowired
    private OrganizationConvert organizationConvert;

    @Override
    public void add(UserAffiliate userAffiliate, String operator) {
        //删除原有关联关系
        userAffiliateDBRepository.deleteAllByUname(userAffiliate.getUname());

        //添加新的关联关系
        if(userAffiliate.getAffiliates()!=null && !userAffiliate.getAffiliates().isEmpty()){
            for(AffiliateInfo affiliateInfo : userAffiliate.getAffiliates()){
                UserAffiliateEntity userAffiliateEntity = new UserAffiliateEntity();
                userAffiliateEntity.setUname(userAffiliate.getUname());
                userAffiliateEntity.setInstitutionNumber(affiliateInfo.getInstitutionNumber());
                userAffiliateEntity.setHisCode(affiliateInfo.getHisCode());
                userAffiliateDBRepository.save(userAffiliateEntity);
            }
        }
    }

    @Override
    public UserAffiliate findByKey(String s) {
        List<UserAffiliateEntity> userAffiliateEntities = userAffiliateDBRepository.findAllByUname(s);
        if(userAffiliateEntities==null || userAffiliateEntities.isEmpty()){
            return new UserAffiliate(s, null);
        }
        Set<AffiliateInfo> affiliates = new HashSet<>();
        for(UserAffiliateEntity userAffiliateEntity : userAffiliateEntities){
            OrganizationEntity organizationEntity = organizationDBRepository.
                    findFirstByInstitutionNumberAndDelFalse(userAffiliateEntity.getInstitutionNumber());
            if(organizationEntity == null){
                continue;
            }
            affiliates.add(new AffiliateInfo(organizationConvert.convert(organizationEntity), userAffiliateEntity.getHisCode()));
        }
        return new UserAffiliate(s, affiliates);
    }

    @Override
    public void delete(String s, String operator) {

    }
}
